package com.software.car.service;

import com.github.pagehelper.PageHelper;
import com.software.car.api.ModelService;
import com.software.car.common.entity_change.ModelChangeMapper;
import com.software.car.common.entity_change.ModelOptionChangeMapper;
import com.software.car.common.enums.ModelTypeEnum;
import com.software.car.common.enums.ResultEnum;
import com.software.car.common.enums.SimpleStateEnum;
import com.software.car.common.exception.MyException;
import com.software.car.common.utils.CommonPage;
import com.software.car.common.utils.FormChangeUtils;
import com.software.car.common.utils.SnowflakeIdGenerator;
import com.software.car.entity.dto.InsertModelDto;
import com.software.car.entity.dto.ModelOptionDto;
import com.software.car.entity.dto.TableOptionDto;
import com.software.car.entity.pro.ModelOptionPro;
import com.software.car.entity.vo.ModelOptionVo;
import com.software.car.entity.vo.ModelTableOptionVo;
import com.software.car.entity.vo.ModelVo;
import com.software.car.entity.vo.ModelSimpleVo;
import com.software.car.entity.po.ModelPo;
import com.software.car.mapper.ModelMapper;
import com.software.car.mapper.ModelOptionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 魔法はまだ解けない
 * @Date 2021/2/22
 */
@Service
public class ModelServiceImpl implements ModelService {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    ModelOptionMapper modelOptionMapper;

    //添加之前需处理
    @Override
    public InsertModelDto addInit(InsertModelDto modelDto){
        modelDto.setId(SnowflakeIdGenerator.getId());
        modelDto.setCreateTime(new Date());
        modelDto.setUpdateTime(new Date());
        //将每个选项默认值转换成字符串
        modelDto.getOptions().forEach(e->{
            e.setId(SnowflakeIdGenerator.getId());
            //如果选项是单选框
            if(e.getType()==2){
                e.setDefaultValues(FormChangeUtils.getString(e.getOptionValuePoList()));
            }
            e.setState(1);
        });
        modelDto.getTableOptions().forEach(e->{
            e.setType(10);
            e.setState(1);
            e.setId(SnowflakeIdGenerator.getId());
        });
        modelDto.setCreateTime(new Date());
        return modelDto;
    }

    @Override
    public InsertModelDto updateInit(InsertModelDto modelDto) {
        ModelVo model = getModel(modelDto.getId(), null);
        if(model==null){
            throw new MyException(ResultEnum.MODEL_NOT_EXIT);
        }
        //设置回原来的id、创建时间、创建人
        modelDto.setId(model.getId());
        modelDto.setCreateTime(model.getCreateTime());
        modelDto.setCreateUser(model.getCreateUser());
        modelDto.setUpdateTime(new Date());

        List<ModelOptionVo> oldOptions = model.getOptions();
        List<ModelOptionDto> newOptions = modelDto.getOptions();
        List<TableOptionDto> tableOptions = modelDto.getTableOptions();

        newOptions.forEach(e->{
            //如果选项是单选框
            if(e.getType()==2){
                e.setDefaultValues(FormChangeUtils.getString(e.getOptionValuePoList()));
            }
            if(e.getId()==null){
                e.setId(SnowflakeIdGenerator.getId());
                return;
            }
            //保留原来的选项id
            for (int i = 0; i < oldOptions.size(); i++) {
                if(e.getId().equals(oldOptions.get(i).getId())){
                    e.setValueType(oldOptions.get(i).getValueType());
                    break;
                }
            }
        });
        tableOptions.forEach(e->{
            e.setType(10);
            if(e.getId()==null){
                e.setId(SnowflakeIdGenerator.getId());
                return;
            }
            //保留原来的选项id
            for (int i = 0; i < oldOptions.size(); i++) {
                if(e.getId().equals(oldOptions.get(i).getId())){
                    e.setValueType(oldOptions.get(i).getValueType());
                    break;
                }
            }
        });
        return modelDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addModel(InsertModelDto modelDto) {
        modelMapper.addModel(modelDto);
        modelOptionMapper.addOptions(modelDto.getOptions(),modelDto.getId());
        modelOptionMapper.addTableOptions(modelDto.getTableOptions(),modelDto.getId());

        return modelDto.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateModel(InsertModelDto modelDto) {
        int i = deleteModel(modelDto.getId());
        String id = addModel(modelDto);
        return id;
    }

    @Override
    public CommonPage getSimpleModel(Integer state, Integer type, Integer page, Integer pageSize,String name) {
        PageHelper.startPage(page, pageSize);
        List<ModelSimpleVo> simpleModel = getSimpleModel(state, type,name);
        CommonPage wrap = CommonPage.wrap(simpleModel);
        List<ModelSimpleVo> list = (List<ModelSimpleVo>) wrap.getList();
        List<ModelSimpleVo> collect = list.stream().map(ModelChangeMapper.INSTANCE::simpleModelToVo).collect(Collectors.toList());
        wrap.setList(collect);
        return wrap;
    }

    @Override
    public ModelVo getModel(String modelId,Integer state) {
        ModelVo model = modelMapper.getModelDetail(modelId);
        if(model==null){
            throw new MyException(ResultEnum.MODEL_NOT_EXIT);
        }
        model = ModelChangeMapper.INSTANCE.modelToVo(model);
        //获取第一种
        List<ModelOptionPro> modelOptions = modelOptionMapper.getModelOptions(modelId,state,null);
        //获取第二种
        List<ModelOptionPro> tableOptions = modelOptionMapper.getModelOptions(modelId,state,10);

        List<ModelOptionVo> collect = modelOptions.stream().
                map(ModelOptionChangeMapper.INSTANCE::modelProTOVo).
                collect(Collectors.toList());

        List<ModelTableOptionVo> collect1 = tableOptions.stream().
                map(ModelOptionChangeMapper.INSTANCE::modelTableOptionProTOVo).
                collect(Collectors.toList());
        //set回去
        model.setTableOptions(collect1);
        model.setOptions(collect);
        System.out.println(modelOptions);
        return model;

    }

    @Override
    public List<ModelSimpleVo> getSimpleModel(Integer state, Integer type, String name) {
        List<ModelSimpleVo> simpleModels = modelMapper.getSimpleModel(state,type,name);
        List<ModelSimpleVo> collect = simpleModels.stream().map(ModelChangeMapper.INSTANCE::simpleModelToVo).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int updateModel(String modelId,Integer state,String updateId) {
        ModelPo build = ModelPo.builder().
                id(modelId).
                updateUser(updateId).
                state(state).
                build();
        return modelMapper.updateModel(build);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteModel(String modelId) {
        int i = modelMapper.deleteModel(modelId);
        modelOptionMapper.deleteOption(modelId);
        return 0;
    }


}
